package com.banmao.learnandorid.listView;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.banmao.learnandorid.R;
import com.banmao.learnandorid.entity.Person;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class ListViewActivity extends AppCompatActivity {

    private List<Person> dataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

        // 生成数据
        for (int i = 0; i < 50; i++) {
            String uuid = UUID.randomUUID().toString().replace("-", "");
            dataList.add(new Person("张" + i, uuid, Long.valueOf(uuid.hashCode())));
        }

        MyAdapter myAdapter = new MyAdapter(dataList, this);

        ListView listView = findViewById(R.id.list_view);
        listView.setAdapter(myAdapter);

        // 监听点击事件，显示name
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView textView = view.findViewById(R.id.list_item);
                if(textView == null) {
                    Log.e("系统错误", "textView为null");
                    Toast.makeText(ListViewActivity.this, "系统错误", Toast.LENGTH_SHORT).show();
                } else {
                    Log.d("测试", textView.getText().toString());
                    Toast.makeText(ListViewActivity.this, textView.getText(), Toast.LENGTH_SHORT).show();
                }
            }
        });

        // 监听长按事件，显示pk
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

                TextView textView = view.findViewById(R.id.list_item);
                if(textView == null) {
                    Log.e("系统错误", "textView为null");
                    Toast.makeText(ListViewActivity.this, "系统错误", Toast.LENGTH_SHORT).show();
                } else {
                    Person item = (Person) adapterView.getItemAtPosition(i);
                    Toast.makeText(ListViewActivity.this, item.getPk().toString(), Toast.LENGTH_SHORT).show();
                }
                // 返回false会执行setOnItemClickListener
                return true;
            }
        });

    }

}